<?php 
namespace Wdl\model;
use Wdl\helper\VillageHelper;
use Wdl\helper\webHelper;

class village extends base{

    public function index()
    {
        $uniacid = $this->uniacid;
        $info = pdo_fetch("select * from ".tablename('kbwdl_area').
        " where uniacid=:uniacid and id=:id and type=3",
        ['uniacid'=> $this->uniacid, 'id'=> $this->_GPC['id']]);

        if($info){
            $info['lvhua'] = empty($info['lvhua']) ? '20%' : $info['lvhua'];
            $info['rongji'] = empty($info['rongji']) ? '1.6' : $info['rongji'];
            $info['chewei'] = empty($info['chewei']) ? '--' : $info['chewei'];
            $info['mainhouse'] = empty($info['mainhouse']) ? '--' : $info['mainhouse'];
            $info['avaprice'] = empty($info['avaprice']) ? '--' : $info['avaprice'];
            $info['telephone'] = empty($info['telephone']) ? '--' : $info['telephone'];
            $info['wuye'] = empty($info['wuye']) ? '--' : $info['wuye'];
            //求均价
            $sum =  pdo_fetch("select sum(loyer) as loyer,sum(superficie) as sf from ".tablename("kbwdl_house").
                " WHERE village_id=:vid AND uniacid=:uniacid AND zhutype=:zhutype",
            ['uniacid'=> $info['uniacid'],'zhutype'=> 0, 'vid'=> $info['id']]);
            if($sum){
                $info['item_per'] = ceil( $sum['loyer'] * 10000 / max(1, $sum['sf']) ) . "元/㎡";
            }
            if(empty($info['thumb'])){
                $info['thumb'] = tomedia("/addons/kbwdl_shequzufang/style/images/house_face_min.jpg");
            }else{
                $info['thumb'] = tomedia($info['thumb']);
            }
            
            $info['slides'][] = array(
                'url'=> "",/*这个是图片链接*/
                'thumb'=> tomedia($info['thumb']),
            );
            if($info['point']){
                $info['point'] = VillageHelper::baidu_map_txmap($info['point']);
                // 小区坐标覆盖
                $m = explode(",", $info['point']);
                $info['longitude'] = $m[0];
                $info['latitude'] = $m[1];
            }      
            //获取小区的周边配套数据
            $mapdata = webHelper::fetch_place_data('village_mapdata','', $info['id']);
            $info['mapdata'] = $mapdata;

            //随机获取 一个经纪人             
           /**联系人信息*/
            $default_avatar = "/addons/kbwdl_shequzufang/style/images/get_avatar.png";
            $info['broker'] = array(
                'id' => 0,
                'avatar' => toimage($default_avatar),
                'nickname'=> $info['publish_name'],
                'mobile'=> $info['linkphone'],
                'company'=> '联系人',
            );
            $broker = pdo_fetch("select * from " . tablename("kbwdl_broker") . " where iscompany=1 and uniacid='$uniacid' ORDER BY RAND()");
            if($broker){
                if (!strpos($broker['avatar'], "/")) {
                    $broker['avatar'] = $default_avatar;
                }
                $broker['avatar'] = toimage($broker['avatar']);
                $broker['nickname'] = !empty($broker['truename']) ? $broker['truename']: $broker['nickname'];
               
                $info['broker'] = $broker;
            }
            return  $this->msg(0, '', $info);
        }

        
        return  $this->msg(1, '暂无小区数据', []);
    }

    public function map()
    {
        $uniacid = $this->uniacid;
        $ret = pdo_fetchall("select `id`, `area`, `name`,  `point` from ".tablename("kbwdl_area"). " where `point` <>'' and uniacid='$uniacid' ");
        $items = array();
        foreach($ret as $key => $val){
            if($val['point']){
                $val['point'] = VillageHelper::baidu_map_txmap($val['point']);
                // 小区坐标覆盖
                $m = explode(",", $val['point']);
                $lon = $m[0];
                $lat = $m[1];
                //房源数量
                $count = VillageHelper::get_village_house_nums($val['area'] , $val['name'], false);
                if($count){
                    $items[] = array(
                        'id'=> intval( $val['id']), 
                        'title'=> $val['name'] ."({$count})", 
                        'longitude' => $lon, 
                        'latitude'=> $lat
                    );
                }
               
            }
        }
        return  $this->msg(0, '', $items);
    }
}